package com.twu.biblioteca.app;

import com.twu.biblioteca.exception.CheckoutFailedException;
import com.twu.biblioteca.service.MenuService;
import com.twu.biblioteca.service.UserService;

import java.io.PrintStream;
import java.util.Scanner;

import static javafx.application.Platform.exit;

public class BibliotecaApp {

    private static final String WELCOME_MESSAGE = "Welcome to Biblioteca!\n";
    private static final String PROMPT_INPUT = "Input the index of the menu (0 to abort):";
    private static final String GOODBYE = "Have a good day!\n";
    private static MenuService menuService = new MenuService();
    private static UserService userService = new UserService();
    private static PrintStream printStream = System.out;
    private static Scanner scanner = new Scanner(System.in);
    private static boolean isLogin = false;

    public static void main(String[] args) throws CheckoutFailedException {
        printWelcome();
        isLogin = userService.login();
        if (!isLogin) {
            printGoodbye();
            return;
        }
        start();
    }

    private static void start() throws CheckoutFailedException {
        menuService.printMenu();
        while (true) {
            printPrompts();
            int input = scanner.nextInt();
            if (input == 0) {
                break;
            } else {
                menuService.handle(input);
            }
        }
        printGoodbye();
    }

    private static void printPrompts() {
        printStream.print(PROMPT_INPUT);
    }

    private static void printGoodbye() {
        printStream.println(GOODBYE);
    }

    private static void printWelcome() {
        printStream.println(WELCOME_MESSAGE);
    }

    public void setPrintStream(PrintStream printStream) {
        BibliotecaApp.printStream = printStream;
    }

    public void setMenuService(MenuService menuService) {
        BibliotecaApp.menuService = menuService;
    }

    public void setScanner(Scanner scanner) {
        BibliotecaApp.scanner = scanner;
    }

    public void setUserService(UserService userService) {
        BibliotecaApp.userService = userService;
    }

}
